﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Web.Security;
using Taobao.EF;
using Taobao.Models;
using Taobao.Public;
using Taobao.Util;

namespace Taobao.Manger
{
    public class HomeManger
    {
        DbEntity db = new DbEntity();

        /// <summary>
        /// 根据搜索词查出 店铺信息
        /// </summary>
        /// <param name="shopName"></param>
        /// <returns></returns>
        public List<Shop> GetHtmlByKey(string shopName)
        {
            List<Shop> list = new List<Shop>();

            string url = "https://shopsearch.taobao.com/search?app=shopsearch&q=";
            url += HttpUtility.UrlEncode(shopName);
            var html = SpiderMethod.GetHtmlByHttpHelper(url);
            var con = SpiderMethod.GetContent(html);
            if (string.IsNullOrEmpty(con))
            {
                return null;
            }
            list = CustomClass.JsonToList<Shop>(con);
            return list;
        }
        /// <summary>
        /// 获取商品详细信息
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public List<auctionsInshop> GetDetailList(string url)
        {
            url = "https:" + url;
            var html = SpiderMethod.GetHtmlByHttpHelper(url);
            Rule rule = new Rule(html, url);
            var list = rule.GetProductList();
            //设置缓存
            if (list != null && list.Count > 0)
            { 
                CustomClass.InserNoabsoCahce(Constant.productdetail, list, 30);
                CustomClass.InserNoabsoCahce(Constant.lastShopUrl, url, 30); 
            }
            return list;
        }
        /// <summary>
        /// 将选中的商品保存到数据库
        /// </summary>
        /// <param name="url"></param>
        /// <param name="prolist"></param>
        /// <returns></returns>
        public bool SaveProduct(List<string> url, List<auctionsInshop> prolist, int userId)
        {
            List<ProductInfo> piList = new List<ProductInfo>();

            for (int index = 0; index < url.Count; index++)
            {
                var item = prolist.Where(o => o.url == url[index].Trim()).FirstOrDefault();
                ProductInfo pinfo = new ProductInfo();
                if (item != null)
                {
                    var piItem = (auctionsInshop)item;

                    pinfo = CustomClass.ConvertToProIn<auctionsInshop>(piItem, userId);
                    piList.Add(pinfo);
                }
            }
            if (piList == null || piList.Count == 0)
            {
                return false;
            }
            try
            {
                db.ProductInfoes.AddRange(piList);
                db.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }

        }
        public void SetAuthCookie(string userId, bool iskeep)
        {
            var ticket = new FormsAuthenticationTicket(2, userId, DateTime.Now, DateTime.Now.AddDays(14), true, "", FormsAuthentication.FormsCookiePath);
            string ticketEncrypted = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie;
            if (iskeep)
            {
                cookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticketEncrypted)
                {
                    HttpOnly = true,
                    Path = FormsAuthentication.FormsCookiePath,
                    Secure = FormsAuthentication.RequireSSL,
                    Expires = ticket.Expiration,
                    Domain = FormsAuthentication.CookieDomain

                };
            }
            else
            {
                cookie = new HttpCookie(FormsAuthentication.FormsCookieName, ticketEncrypted)
                {
                    HttpOnly = true,
                    Path = FormsAuthentication.FormsCookiePath,
                    Secure = FormsAuthentication.RequireSSL,
                    //Expires = ticket.Expiration,//无过期时间的，浏览器关闭后失效
                    Domain = FormsAuthentication.CookieDomain
                };
            }

            HttpContext.Current.Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
            HttpContext.Current.Response.Cookies.Add(cookie);
            var mm = HttpContext.Current.User.Identity.Name;
        }
    }
}